package com.feng.show.controller;


import com.alibaba.fastjson.JSON;
import com.feng.show.comm.ServerResponse;
import com.feng.show.entity.Agent;
import com.feng.show.entity.Merchant;
import com.feng.show.service.IAgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/agent")
public class AgentController {
    /*
    产地
     */

    @Autowired
    private IAgentService agentServiceIpml;


    @PostMapping("/getAll")
    public ServerResponse<List<Agent>> getAll(@RequestParam(value = "userId") String userId)

    {
        userId="10001";
        if(userId==null){
            return ServerResponse.createByErrorMsg("请求参数错误");
        }
        return agentServiceIpml.getAgentList(userId);

    }


    @PostMapping("/add")
    public ServerResponse<String> addAgent(

            @RequestParam(value = "agent") String data,
            @RequestParam(value = "userId") String userId)

    {
        Agent agent;
        if (data == null) {
            return ServerResponse.createByErrorMsg("参数错误");
        }

        try{
            agent= JSON.parseObject(data,Agent.class);
        }catch(Exception e) {
            return ServerResponse.createByErrorMsg("参数错误");
        }
        return agentServiceIpml.addMerchant(userId,agent);

    }

    @PostMapping("/modify")
    public ServerResponse<String> modifyAgent(

            @RequestParam(value = "agent") String data,
            @RequestParam(value = "userId") String userId)

    {
        Agent agent;
        if (data == null) {
            return ServerResponse.createByErrorMsg("参数错误");
        }

        try{
            agent= JSON.parseObject(data,Agent.class);
        }catch(Exception e) {
            return ServerResponse.createByErrorMsg("参数错误");
        }
        return agentServiceIpml.modifyMerchant(userId,agent);

    }

  @PostMapping("/delMerchant")
  public ServerResponse<String> delMerchant(

    @RequestParam(value = "agent") String data,
    @RequestParam(value = "userId") String userId)

  {
    Agent agent;
    if (data == null) {
      return ServerResponse.createByErrorMsg("参数错误");
    }

    try{
      agent= JSON.parseObject(data,Agent.class);
    }catch(Exception e) {
      return ServerResponse.createByErrorMsg("参数错误");
    }
    return agentServiceIpml.delMerchant(userId,agent);

  }





    //添加agent


//    @PostMapping("/getAreaList")
//    public ServerResponse<List<ProductArea>> getProductAreaList(
//
//            @RequestParam(value = "userId") String userId)
//
//    {
//
//        userId="10001";
//        if(userId==null){
//            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"参数错误");
//
//        }else{
//            return productAreaSerViceImpl.getAreaList(userId) ;
//        }
//
//    }










//        if(no==null||name==null||des==null){
//            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"参数错误");
//
//        }else{
//            return addProductTypeService.addProductType(name,no,des);
//        }

    }
//
//    @PostMapping("getProductTypeList")
//    public ServerResponse<List<ProductType>> getProductTypeList(
//
//            @RequestParam(value = "userId") String userId)
//
//    {
//
//        userId="10001";
//        if(userId==null){
//            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"参数错误");
//
//        }else{
//            return addProductTypeService.getProductTypeList(userId);
//        }
//
//    }








